#include "clockstatcalendarmodel.h"

ClockStatCalendarModel::ClockStatCalendarModel(QObject *parent ) : QAbstractListModel(parent)
{

}

int ClockStatCalendarModel::rowCount(const QModelIndex &) const
{
    return m_contacts.count();
}

QVariant ClockStatCalendarModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < rowCount())
        switch (role) {
        case DateRole: return m_contacts.at(index.row()).date;
        case StatusRole: return m_contacts.at(index.row()).status;
        default: return QVariant();
        }
    return QVariant();
}

QHash<int, QByteArray> ClockStatCalendarModel::roleNames() const
{
    static const QHash<int, QByteArray> roles {
        { DateRole, "date" },
        { StatusRole, "status" },
    };
    return roles;
}

QVariantMap ClockStatCalendarModel::get(int row) const
{
    const Contact contact = m_contacts.value(row);
    return { {"date", contact.date}, {"status", contact.status} };
}

void ClockStatCalendarModel::append(const Contact contact)
{
    int row = 0;
    while (row < m_contacts.count())
        ++row;

    beginInsertRows(QModelIndex(), row, row);
    m_contacts.insert(row,contact);
    calendar_keys.insert(contact.date,row);
    endInsertRows();
}

bool ClockStatCalendarModel::hasByDate(QString date)
{
    return calendar_keys.contains(date);
}

QVariantMap ClockStatCalendarModel::getByDate(QString date)
{
    int row = calendar_keys.value(date);
    return get(row);
}

